package pers.feng1240.blog.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import pers.feng1240.blog.entities.Manager;
import pers.feng1240.blog.service.ManagerService;


/*
 * 管理员登录
 */
@Controller
public class ManagerController {

    @Resource
    private ManagerService managerService; //业务逻辑
    
    /*
     * 登录
     */
    @RequestMapping("login")
    public String login(String account,String password,HttpServletRequest request){
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("account", account);
        map.put("password", password);
        Manager manager = managerService.login(map);
        if(manager == null){
            request.setAttribute("message", "用户名密码错误！");
            return "forward:index.jsp";
        }else{
            request.getSession().setAttribute("manager", manager);
            return "redirect:admin/toIndex.do";
        }
    }
    
    /*
     * 跳转到后台首页
     */
    @RequestMapping("admin/toIndex")
    public String toIndex(){
    	return "forward:index.jsp";
    }
    
    /*
     * 用户注销
     */
    @RequestMapping("admin/logout")
    public String logout(HttpServletRequest request){
        //删除session
        request.getSession().removeAttribute("manager");
        //返回到index.jsp
        return "redirect:../login.jsp";
    }
}
